home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / PaletteAnimation / PaletteAnimation.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  5.1 KB  |  191 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        PaletteAnimation.c
  3.  
  4.     Contains:    This is a palette animation demonstration
  5.  
  6.     Written by: RICHARD P. COLLYER      
  7.  
  8.     Copyright:    Copyright © 1990-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/13/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23. #include    <Quickdraw.h>
  24. #include    <Windows.h>
  25. #include    <Events.h>
  26. #include     <GestaltEqu.h>
  27. #include    <OSEvents.h>
  28. #include    <Palettes.h>
  29. #include    <SegLoad.h>
  30. #include    <TextUtils.h>
  31.  
  32. extern _DataInit();
  33.  
  34. #define    TRUE            0xFF
  35. #define    FALSE            0
  36.  
  37. #define    clutID            150
  38. #define    numcolor        256
  39.  
  40. Rect                WinMinusScroll;
  41. WindowPtr            myWindow;
  42. CTabHandle            mycolors;
  43. PaletteHandle        srcPalette;
  44. Boolean                DoneFlag;
  45.  
  46. #ifdef powerc
  47.    QDGlobals    qd;
  48. #endif
  49. void Draw();
  50. void init();
  51. /*______________________________________________________*/
  52. /*             Set Up Usage of Palette                  */
  53. /*______________________________________________________*/
  54. void Draw()
  55. /* The drawing routine is a collection of 253 random Rects which are colored with 
  56. the corresponding color in the color table. */
  57. {
  58.     float                    left, top, right, bottom;
  59.     Rect                    CRect;
  60.     int                        i;
  61.  
  62.     EraseRect (&WinMinusScroll);
  63.     for (i = 1; i < numcolor-2; ++i) {
  64.         do {
  65.             left = Random() / 32767.0 * WinMinusScroll.right;
  66.             if (left < 0)
  67.                 left = -left;
  68.             } while (left > WinMinusScroll.right - 40);
  69.             
  70.         do {
  71.             right = Random() / 32767.0 * WinMinusScroll.right;
  72.             if (right < 0)
  73.                 right = -right;
  74.             } while (right < left);
  75.             
  76.         do {
  77.             top = Random() / 32767.0 * WinMinusScroll.bottom;
  78.             if (top < 0)
  79.                 top = -top;
  80.             } while (top > WinMinusScroll.bottom - 40);
  81.             
  82.         do {
  83.             bottom = Random() / 32767.0 * WinMinusScroll.bottom;
  84.             if (bottom < 0)
  85.                 bottom = -bottom;
  86.             } while (bottom < top);
  87.         SetRect(&CRect, left, top, right, bottom);
  88.         PmForeColor(i);
  89.         FrameRect (&CRect);
  90.         PaintRect (&CRect);
  91.         }
  92. }
  93.  
  94. /*______________________________________________________*/
  95. /*               Initialization traps                   */
  96. /*______________________________________________________*/
  97. void init()
  98. {
  99.     Rect                BaseRect;
  100.     OSErr                err;
  101.     long                feature;
  102.  
  103.     UnloadSeg(_DataInit);
  104.     InitGraf(&qd.thePort);
  105.     FlushEvents(everyEvent, 0);
  106.     InitWindows();
  107.     InitCursor();
  108.     
  109.     DoneFlag = FALSE;
  110. /*______________________________________________________*/
  111. /*            Use Gestalt to find 32BQD                 */
  112. /*______________________________________________________*/
  113.     err = Gestalt(gestaltQuickdrawVersion, &feature);
  114.     if (!err) {
  115.         if ((feature & 0x0f00) != 0x0200)
  116.             DoneFlag = TRUE;
  117.         }
  118.     else 
  119.         DoneFlag = TRUE;
  120. /*______________________________________________________*/
  121. /*                     Set Rects                        */
  122. /*______________________________________________________*/
  123.     SetRect(&BaseRect, 40, 60, 472, 282);
  124.     SetRect(&WinMinusScroll, BaseRect.left-40, BaseRect.top-60, BaseRect.right-60, 
  125.                 BaseRect.bottom - 80);
  126. /*______________________________________________________*/
  127. /*        Open Window & set Palette & Picture           */
  128. /*______________________________________________________*/
  129.     mycolors = GetCTable (clutID);
  130.  
  131.     myWindow = NewCWindow(nil, &BaseRect, "\pUsing Palette Manager", TRUE, documentProc, 
  132.                             (WindowPtr) -1, TRUE, 150);
  133.     SetPort((WindowPtr) myWindow);
  134.  
  135.     srcPalette = NewPalette (numcolor, mycolors, pmAnimated, 0x1500);
  136.     SetPalette ((WindowPtr) myWindow, srcPalette, TRUE);
  137.     
  138.     Draw();
  139. }
  140.  
  141. main()
  142. {
  143.     EventRecord     myEvent;
  144.     int                yieldTime = 0, Counter;
  145.     RGBColor        changecolor, black = {0,0,0};
  146.     long            startTicks, finalTicks;
  147.     Str255            countstr;
  148. /*______________________________________________________*/
  149. /*                   Main Event loop                    */
  150. /*______________________________________________________*/
  151.     init();    
  152.     startTicks = TickCount();
  153.     Counter = 0;
  154.     for ( ;; ) {
  155.         if (DoneFlag) {
  156.             DoneFlag = FALSE;
  157.             RGBForeColor (&black);
  158.             NumToString(finalTicks,(unsigned char*)&countstr);
  159.             MoveTo (20,20);
  160.             DrawString ("\pThe Number of Tick = ");
  161.             MoveTo (150,20);
  162.             DrawString ((unsigned char*)&countstr);
  163.             for (;;) {
  164.                 if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  165.                     switch (myEvent.what) {
  166.                         case mouseDown:
  167.                         case keyDown:
  168.                         case autoKey:
  169.                             DoneFlag = TRUE;
  170.                             break;
  171.                         default:
  172.                             break;
  173.                         }
  174.                     }
  175.                 if (DoneFlag)
  176.                     ExitToShell();
  177.                 }
  178.             }
  179.             
  180.         /* Animate the colors one step for eash event loop */
  181.         GetEntryColor (srcPalette, 1, &changecolor);
  182.         AnimatePalette (myWindow, mycolors, 2, 1, numcolor - 2);
  183.         AnimateEntry (myWindow, numcolor - 1, &changecolor);
  184.         Palette2CTab (srcPalette, mycolors);
  185.         
  186.         if (++Counter == 1000) {
  187.             finalTicks = TickCount() - startTicks;
  188.             DoneFlag = TRUE;
  189.             }
  190.         }
  191. }